package com.atung.parking.controller;

import com.atung.parking.config.auth.AuthUserMangerContext;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.security.JwtTokenUtil;
import com.atung.parking.request.AdminAuthLoginRequest;
import com.atung.parking.request.WxLoginRequest;
import com.atung.parking.service.AuthServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/auth")
@Api(tags = "认证中心")
public class AuthController {

    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private AuthServiceImpl authService;


    @PostMapping("/admin/login")
    @ApiOperation("管理员登录")
    public ApiResult administratorLogin(@RequestBody @Valid AdminAuthLoginRequest request, HttpServletRequest servletRequest) {
        return authService.administratorLoginService(request, servletRequest);
    }

    @PostMapping("/user/login")
    @ApiOperation("(小程序)用户登录")
    public ApiResult userLogin(@RequestBody @Valid WxLoginRequest wxLoginRequest, HttpServletRequest servletRequest) {
       return authService.wxLoginService(wxLoginRequest,servletRequest);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ApiResult logout() {
        try {
            jwtTokenUtil.clearAuthUserEntity(AuthUserMangerContext.getAuthUserEntity());
        } catch (Exception e) {
        }
        // 告诉客户端删除Token
        return ApiResult.ok();
    }
}
